﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebComponents.Data;
using WebComponents;
using WebComponents.Security;
using GBIT.Web;

public partial class ThemTinTuc : UserControlBase {
  int articleID = 0;
  protected void Page_Load(object sender, EventArgs e) {
    txtTrichDan.ToolbarStartExpanded = false;
    this.Title = "Tin tức";
    this.TaskTitle = "Thêm mới";
    if (Request.Params["id"] != null)
      articleID = ConvertType.ToInt(Request.Params["id"]);
    if (!Page.IsPostBack) {
      if (articleID > 0) {
        ArticleInfo article = ArticleController.GetByArticleID(articleID);
        if (article != null) {
          txtTitle.Text = article.Title;
          txtTrichDan.Value = article.Description;
          txtContent.Value = article.Content;
          txtOrderId.Text = article.OrderId.ToString();
          ImagePath.ImagePath = article.ImageUrl;
          ImagePath.ImageUrl = article.ImageUrl;
          chkIsActive.Checked = article.IsApproved;
          this.TaskTitle = "Cập nhật";
          cmbStatus.SelectedIndex = -1;
          foreach (ListItem item in cmbStatus.Items) {
            if (ConvertType.ToInt(item.Value) == article.StatusID)
              item.Selected = true;
          }
          if (article.CultureId == "vi-VN")
            cmbLanguage.SelectedIndex = 0;
          else
            cmbLanguage.SelectedIndex = 1;
        }
      } else
        txtOrderId.Text = ArticleController.MaxOrderId(1).ToString();
      cmbChuyenMuc.Items.Clear();
      cmbChuyenMuc.Items.Add(new ListItem("", ""));
      Global.LoadCategory(cmbLanguage.SelectedValue, ref cmbChuyenMuc, CatetoryType.News);
    }
    this.SaveEnabled = true;   
  }
  protected override void OnSaveRecord() {
    ArticleInfo article = null;
    if (articleID > 0)
      article = ArticleController.GetByArticleID(articleID);
    if (article == null)
      article = new ArticleInfo();
    article.Title = txtTitle.Text;
    article.Description = txtTrichDan.Value;
    article.Content = txtContent.Value;
    article.OrderId = ConvertType.ToInt(txtOrderId.Text);
    article.ImageUrl = ImagePath.ImagePath;
    article.IsApproved = chkIsActive.Checked;
    article.CultureId = cmbLanguage.SelectedValue;
    article.StatusID = ConvertType.ToInt(cmbStatus.SelectedValue);
    if (articleID <= 0) {
      article.AddedDate = DateTime.Now;
      article.CateID = ToInt(cmbChuyenMuc.SelectedValue);
      ArticleController.Insert(article);
      Security.ShowClientMessage(Page, "Thêm tin mới thành công", string.Format("?mod={0}", WebComponents.ConvertType.Encode("TinTuc/TinTucList.ascx")));
    } else {
      article.UpdatedDate = DateTime.Now;
      ArticleController.Update(article);
      Security.ShowClientMessage(Page, "Cập nhật thông tin thành công", string.Format("?mod={0}", WebComponents.ConvertType.Encode("TinTuc/TinTucList.ascx")));
    }
  }  
}
